﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Data;
using DuSQL.API;
using System.Threading;
using DuSQL.ViewModel;

namespace DuSQL.Control
{
    /// <summary>
    /// ConnectionList.xaml 的交互逻辑
    /// </summary>
    public partial class ConnectionList : UserControl
    {
        public ConnectionList()
        {
            InitializeComponent();
            AllConnectsViewModel model = new AllConnectsViewModel();
            this.DataContext = model;
            Comm.ChangeDataEvent += new EventHandler(Comm_ChangeDataEvent);
        }
        void Comm_ChangeDataEvent(object sender, EventArgs e)
        {
            //treeConnection.ItemsSource = DuSQL.API.Comm.MyConnections;
            AllConnectsViewModel model = new AllConnectsViewModel();
            this.DataContext = model;
        }

        private void mnduEdit_Click(object sender, RoutedEventArgs e)
        {
            //if (Comm.CurSelNode.nodetype) 
            //MessageBox.Show("幸福的");
            //treeConnection.
        }

        private void ContextMenu_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            
        }
       
        private void treeviewitem_previewmouserightbuttondown(object sender, MouseButtonEventArgs e)
        {
            var treeviewitem = visualupwardsearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
            if (treeviewitem != null)
            {         
                switch(treeviewitem.Header.ToString())
                {
                    case "DuSQL.ViewModel.TableInfoViewModel":
                        {
                            treeviewitem.ContextMenu = gridlist.Resources["menuTable"] as ContextMenu;
                            DuSQL.ViewModel.TableInfoViewModel model = treeviewitem.Header as TableInfoViewModel;
                            DuSQL.API.Comm.CurSelNode.name = model.TableName;
                            DuSQL.API.Comm.CurSelNode.nodetype = model.NodeType;
                            break;
                        }
                    case "DuSQL.ViewModel.ViewInfoViewModel":
                        {
                            treeviewitem.ContextMenu = gridlist.Resources["menuTable"] as ContextMenu;
                            DuSQL.ViewModel.ViewInfoViewModel model = treeviewitem.Header as ViewInfoViewModel;
                            DuSQL.API.Comm.CurSelNode.name = model.ViewName;
                            DuSQL.API.Comm.CurSelNode.nodetype = model.NodeType;

                            break;
                        }
                    case "DuSQL.ViewModel.DbConnectViewModel"://连接
                        {
                            treeviewitem.ContextMenu = gridlist.Resources["menuConnect"] as ContextMenu;
                            DuSQL.ViewModel.DbConnectViewModel model = treeviewitem.Header as DbConnectViewModel;
                            DuSQL.API.Comm.CurSelNode.name = model.ConnectName;
                            DuSQL.API.Comm.CurSelNode.nodetype = model.NodeType;
                            break;
                        }
                    default:
                        {
                            treeviewitem.ContextMenu = null;
                            break;
                        }

                    
                       
                }               

                
                treeviewitem.Focus();
                e.Handled = true;
            }
        }

        static DependencyObject visualupwardsearch<t>(DependencyObject source)
        {
            while (source != null && source.GetType() != typeof(t))
                source = VisualTreeHelper.GetParent(source);

            return source;
        }

        private void SelectTop100_Click(object sender, RoutedEventArgs e)//选择前100行
        {
            MenuItem item = sender as MenuItem;
            ContextMenu menu = item.Parent as ContextMenu;            
            DuSQL.ViewModel.TableInfoViewModel tbview=treeConnection.SelectedItem as DuSQL.ViewModel.TableInfoViewModel;     
            Grid parent = this.Parent as Grid;            
            Frame frame = parent.FindName("MainFrame") as Frame;       
            DuSQL.UI.EditSql obj = new UI.EditSql(tbview.TableName+".Sql查询窗口",tbview.ConnectStr, tbview.DataBaseType);
            frame.Content = obj;
        }

        private void SelectAll_Click(object sender, RoutedEventArgs e)
        {
            MenuItem item = sender as MenuItem;
            ContextMenu menu = item.Parent as ContextMenu;
            DuSQL.ViewModel.TableInfoViewModel tbview = treeConnection.SelectedItem as DuSQL.ViewModel.TableInfoViewModel;
            Grid parent = this.Parent as Grid;
            Frame frame = parent.FindName("MainFrame") as Frame;
            DuSQL.UI.ShowData obj = new UI.ShowData(tbview.TableName + ".Sql查询窗口", tbview.ConnectStr, tbview.DataBaseType,tbview.TableName);
            frame.Content = obj;
        } 

    }
}
